﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using ASM_Group2.Models;

namespace ASM_Group2.Controllers
{
    public class ProductController : Controller
    {
        double tong,tong2;
        private ApplicationDbContext db = new ApplicationDbContext();
        DateTime ngay;
        // GET: /Product/
        public ActionResult Index()
        {
            var products = db.Products.Include(p => p.Category);
            return View(products.ToList());
        }

        public ActionResult Payment()
        {
            List<CartItem> product = Session["Cart"] as List<CartItem>;
            foreach (var item in product)
            {
                tong += item.Products.Price * item.Quantity;
            }
           
            ngay = @DateTime.Now;
            ListOrder n = new ListOrder() { Date = ngay, TotalPrice = tong };
            OrderDetail o;
            foreach (var item in product)
            {
                tong2 = item.Products.Price * item.Quantity;
                o = new OrderDetail() { OrderId = n.OrderListId, ProductId = item.Products.Id , Quantity =item.Quantity, Totalprice = tong2};
                db.OrderDetails.Add(o);
            }

            ViewBag.to = tong;


            db.ListOrders.Add(n);
            db.SaveChanges();
            return View();
        }


        public ActionResult ViewCart()
        {
            List<CartItem> product = Session["Cart"] as List<CartItem>;
            foreach (var item in product)
            {
                tong += item.Products.Price * item.Quantity;
            }
            ViewBag.to = tong;
            ViewBag.ca = product;
            return View();


        }

        public ActionResult AddCart(int? id)
        {


            Product product = db.Products.Find(id);
            List<CartItem> sp;
            if (product == null)
            {
                return HttpNotFound();
            }
            if (Session["Cart"] == null)
            {
                sp = new List<CartItem>();
                CartItem n = new CartItem { Products = product, Quantity = 1 };
                sp.Add(n);
                ViewBag.total = n.Products.Price * n.Quantity;

                Session["Cart"] = sp;
            }
            else
            {


                sp = Session["Cart"] as List<CartItem>;
                CartItem n;
                bool found = false;
                foreach (var item in sp)
                {
                    if (id == item.Products.Id)
                    {
                        item.Quantity++;
                        found = true;
                    }

                }
                if (!found)
                {
                    n = new CartItem { Products = product, Quantity = 1 };
                    sp.Add(n);
                }


                Session["Cart"] = sp;

            }



            ViewBag.Cart = Session["Cart"];
            return RedirectToAction("ViewCart");

        }
    


        // GET: /Product/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Product product = db.Products.Find(id);
            if (product == null)
            {
                return HttpNotFound();
            }
            return View(product);
        }

        // GET: /Product/Create
        public ActionResult Create()
        {
            ViewBag.CategoryId = new SelectList(db.Categories, "CategoryId", "NameCategory");
            return View();
        }


        public ActionResult Category(int cid)
        {
            var product = db.Products.Where(p => p.CategoryId == cid);
            return View(product.ToList());
        }
        // POST: /Product/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "Id,Name,Price,Size,Color,Image,Quantity,CategoryId")] Product product, HttpPostedFileBase image)
        {
            if (ModelState.IsValid)
            {
                if (image != null)
                {
                    string path = Server.MapPath("~/Images/" + image.FileName);

                    product.Image = image.FileName;
                    image.SaveAs(path);
                }

                db.Products.Add(product);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.CategoryId = new SelectList(db.Categories, "CategoryId", "NameCategory", product.CategoryId);
            return View(product);
        }

        // GET: /Product/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Product product = db.Products.Find(id);
            if (product == null)
            {
                return HttpNotFound();
            }
            ViewBag.CategoryId = new SelectList(db.Categories, "CategoryId", "NameCategory", product.CategoryId);
            return View(product);
        }

        // POST: /Product/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include="Id,Name,Price,Size,Color,Image,Quantity,CategoryId")] Product product)
        {
            if (ModelState.IsValid)
            {
                db.Entry(product).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.CategoryId = new SelectList(db.Categories, "CategoryId", "NameCategory", product.CategoryId);
            return View(product);
        }

        // GET: /Product/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Product product = db.Products.Find(id);
            if (product == null)
            {
                return HttpNotFound();
            }
            return View(product);
        }

        // POST: /Product/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            Product product = db.Products.Find(id);
            db.Products.Remove(product);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
